package com.zzj.hotel.controller;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.zzj.hotel.core.entity.Room;
import com.zzj.hotel.core.entity.User;
import com.zzj.hotel.service.RoomService;
import com.zzj.hotel.service.UserService;

@Controller
@RequestMapping(value={"/user"})
public class UserController {
	@Autowired UserService userService;
	@Autowired RoomService roomService;
	@RequestMapping(value={"/login"}, method=RequestMethod.GET)
	public ModelAndView toLogin(HttpServletRequest request, Model model){
		model.addAttribute("errorMsg", request.getParameter("errorMsg"));
		return new ModelAndView("login", "command", new User());
	}
	@RequestMapping(value={"/login"}, method=RequestMethod.POST)
	public String login(@ModelAttribute("login") User user, Model model){
		User u = userService.findOne(user.getUsername(), user.getPassword());
		if(u != null){
			return "redirect:../room/list";
		}
		String errorMsg = "error!!!";
		
		return "redirect:login?errorMsg="+errorMsg;
	}
}
